<template>
	<view class="fs12 lh15 padb20">
		<stop isBack>
			<view slot="content">生产报工</view>	
			<view slot="right" class="pos-r bdso bdcf padlr2 padtb1 round2 mar1" @tap="save">
				保存
				<ripple></ripple>
			</view>
		</stop>
		<view class="bcf pad2">
			<view class="list-style-1">
				<view class="flrc">
					<view class="flex1">基础信息</view>
					<view  v-if="emp !== null" class="">操作员：{{emp.name}}[{{emp.sn}}]</view>
				</view>
			</view>
			<view class="mart2" v-if="plan !== null && emp !== null">
				<view class="flrc padtb1 bdtda">
					<view class="c12">产品：</view>
					<view class="flex1 tar">{{plan.product.name}}</view>
				</view>
				<view class="flrc padtb1">
					<view class="c12">计划：{{plan.numDone}} / 完成：{{plan.numNeed}}</view>
					<view class="c12 flex1 tar">排产：</view>
					<view class="">{{plan.sn}}</view>
				</view>
			</view>
			<view class="flrc pos-r" v-if="emps.length > 0" @tap="clearEmps">
				<view class="c12">小组：</view>
				<view class="flex1 tar">
					<text v-for="emp,index in emps">{{(index>0?',':'') + emp.name}}</text>
					<text class="fa fa-window-close marl1"></text>
				</view>
				<ripple></ripple>
			</view>
			<view class="flrc padtb1">
				<view class="c12">单号：</view>
				<view class="flex1 c12 tar">{{code}}</view>
			</view>
			<view class="flrc padtb1" v-if="devices.length > 0" @tap="clearDevices">
				<view class="c12">设备：</view>
				<view class="flex1 tar">
					<text v-for="device,index in devices">{{(index>0?',':'') + device.name}}</text>
					<text class="fa fa-window-close marl1"></text>
				</view>
			</view>
		</view>
		<view class="mart2 bdbda">
			<stab :names="['产量','检验','班组','设备','记录']" v-model="tabIndex" color="text-blue"></stab>
		</view>
		
		<view class="bcf pad2" v-show="tabIndex === 0">
			<CompProcessSelect></CompProcessSelect>
		</view>
		
		<view class="bcf pad2" v-if="tabIndex === 1">
			<CompCheckSelect></CompCheckSelect>
		</view>
		
		<view class="bcf pad2" v-if="tabIndex === 2">
			<CompEmpSelect v-model="emps"></CompEmpSelect>
		</view>
		
		<view class="bcf pad2" v-if="tabIndex === 3">
			<CompDeviceSelect v-model="devices"></CompDeviceSelect>
		</view>
		
		<view class="bcf pad2" v-if="tabIndex === 4">
			<view class="list-style-2">报工记录</view>
			<view class="mart2">
				<view class="flrc bc13 c12 pad2">
					<view class="flex3">工序</view>
					<view class="flex1 tac">成品</view>
					<view>/</view>
					<view class="flex1 tac">使用</view>
					<view>/</view>
					<view class="flex1 tac">修</view>
					<view>/</view>
					<view class="flex1 tac">焊</view>
					<view>/</view>
					<view class="flex1 tac">废</view>
				</view>
				<view class="flrc pad2 bdbdo pos-r ofh" v-for="(paper,index) in pd.paperHistoryList" @tap="mySlide(index)"
				:class="{'bg-green':paper.num - paper.numUsed > 0,'bg-red':paper.num - paper.numUsed < 0}" >
					<view class="flex3" v-if="paper.process">{{paper.process.name}}</view>
					<view class="flex3" v-else>工序丢失</view>
					<view class="flex1 tac">{{paper.num}}</view>
					<view>/</view>
					<view class="flex1 tac">{{paper.numUsed}}</view>
					<view>/</view>
					<view class="flex1 tac">{{paper.numX}}</view>
					<view>/</view>
					<view class="flex1 tac">{{paper.numH}}</view>
					<view>/</view>
					<view class="flex1 tac padtb2">{{paper.numF}}</view>
					<sslide ref="mySlideBox">
						<view class="bca09 c13 padlr2 fs8 c11 op9">
							<view class="flrc">
								<view>员工：</view>
								<view class="flex1">{{paper.emp.name}}</view>
								<view>#{{paper.id}}</view>
							</view>
							<view class="flrc">
								<view>时间：</view>
								<view class="flex1">{{$com.getDateTime(new Date(paper.createAt))}}</view>
							</view>
						</view>
					</sslide>
				</view>
			</view>
		</view>

		<loading :loading="loading"></loading>
		<view class="flrc pos-f b0 l0 r0 bcf">
			<view class="flex2 fs8 lh15 tac c03">
				<view>保存前请注意核对：</view>
				<view>【产品】【人员】【批次】【数量】</view>
			</view>
			<view class="flex1 bg-gradual-green pad4 tac " @tap="save">
				保存
				<ripple></ripple>
			</view>
		</view>
	</view>
</template>

<script>
import log from '../../../assets/js/log'
import CompEmpSelect from './CompEmpSelect.vue'
import CompProcessSelect from './CompProcessSelect.vue'
import CompCheckSelect from './CompCheckSelect.vue'
import CompDeviceSelect from './CompDeviceSelect.vue'
import PublicData from './PublicData'
import scan from '../../../mixin/scan';

export default {
	mixins:[scan],
	components:{CompEmpSelect,CompProcessSelect,CompCheckSelect,CompDeviceSelect},
	data() {
		return {
			code:"",
			pd:PublicData,
			loading:true,
			loadingWait:3,
			dataReady:false,
			emp:null,
			emps:[],
			devices:[],
			flow:null,
			plan:null,
			//processList:[],
			paperHistoryList:[],
			tabIndex:0,
		}
	},
	onLoad(option) {
		this.code = option.code
		this.init()
	},
	onShow() {
		this.ezScanCbFunc = (code)=>{
			console.log("报工扫描",code);
			if (this.pd.canAddSrcPapers){
				this.pd.getSrcPaperByCode(code)
			}
		}
	},
	methods: {
		init(){
			this.pd.clear()
			this.pd.prepareCount = 0
			this.checkAndStart()
			this.emp = this.$com.getLocDataObject("emp")
			this.pd.emp = this.emp
			this.emps = CompEmpSelect.getEmps()
			this.pd.emps = this.emps
			this.devices = CompDeviceSelect.getDevices()
			this.pd.devices = this.devices
		},
		clearEmps(){
			this.emps = []
			this.pd.emps = []
			this.$com.delLocData(PublicData.empsLocalKey)
		},
		clearDevices(){
			this.devices = []
			this.pd.devices = []
			this.$com.delLocData(PublicData.devicesLocalKey)
		},
		save(){
			let flowId = this.pd.getFlowId()
			if(flowId === 0){
				this.$com.showToast("跟踪单未找到")
				return
			}
			let data = {
				paper:{
					flowId,
					processId:this.pd.getProcessId(),
					sn:this.code,
					num:this.pd.num,
					numF:this.pd.numF,
					empId:this.pd.getEmpId(),
					empsIds:this.pd.getEmpsIdArray(),
					deviceId:this.pd.getDeviceId(),
					params:this.pd.getParams(),
					srcPapers:this.pd.getSrcPapers(),
				},
				groups:this.pd.getCheckGroupData(),
			}
			//数据校验
			// 1.num>0
			if (data.paper.num + data.paper.numF   == 0){
				this.$com.showToast(`数量异常:成品(${data.paper.num}),废品(${data.paper.numF})`)
				return
			}
			// 2.员工登录
			if (data.paper.empId == 0){
				this.$com.showToast("请登录")
				this.$win.gointo("/pages/user/phoneLogin")
				return
			}
			// 3.工序
			if (data.paper.processId == 0){
				this.$com.showToast("工序选择异常")
				return
			}
			
			
			this.$api.post("/mes/admin/Paper/save_manuel",data,res=>{
				this.$com.showToast(res.message)
				if(res.code < 3000){
					this.init()
				}
			})
		},
		checkAndStart(){
			this.$api.post("/mes/admin/Flow/check_and_start",{code:this.code},res=>{
				this.flow = res.data
				this.pd.flow = this.flow
				this.pd.prepareCount++
				this.getPlanProcessConfig()
				this.getPlanDeviceConfig()
				this.getPaperHistory()
			})
		},
		getPlanProcessConfig(){
			this.$api.post("/mes/admin/PlanProcessConfig/list",{
				_where:{belongToId:this.flow.planId},
				_order:["sort"],
				_dumps:["belongTo.product","process","params.fields"],
				limit:0
			},res=>{
				if (res.data.lists.length == 0){
					this.$com.showToast("排产工艺配置异常")
					return
				}
				this.pd.processConfigList = res.data.lists
				this.plan = res.data.lists[0].belongTo
				this.pd.plan = this.plan
				this.pd.prepareCount++
				if(this.pd.prepareCount == this.loadingWait){
					this.loading =false
				}
			})
		},
		getPlanDeviceConfig(){
			this.$api.post("/mes/admin/PlanDeviceConfig/list",{
				_where:{belongToId:this.flow.planId},
				_order:["sort"],
				_dumps:["belongTo.product","process","params.fields"],
				limit:0
			},res=>{
				if (res.data.lists.length == 0){
					return
				}else{
					this.pd.planProcesses = res.data.lists.filter()
				}
				this.pd.prepareCount++
				if(this.pd.prepareCount == this.loadingWait){
					this.loading =false
				}
			})
		},
		getPaperHistory(){
			this.$api.post("/mes/admin/Paper/list",{
				_where:{flowId:this.flow.id},
				_order:["-id"],
				_dumps:["process","emp"],
				limit:0,
			},res=>{
				this.pd.paperHistoryList = res.data.lists
				this.pd.prepareCount++
				if(this.pd.prepareCount == this.loadingWait){
					this.loading =false
				}
				if (this.pd.paperHistoryList.length == 0){
					this.pd.canAddSrcPapers = true
				}
			})
		},
	}
}
</script>

<style>

</style>
